import { defineStore } from 'pinia';
import { queryMenus } from '@/layout/menu/api';
import dynamicRouters from '@/router/dynamic-routers';

export const useMenus = defineStore(
  'menus',
  () => {
    const menus = ref([]);
    const keyCollapse = ref(false);
    const tags = ref([]);
    const openKeys = ref([]);
    const selectedKeys = ref([]);

    const getMenus = () => {
      return queryMenus(-1).then((res) => {
        menus.value = res;
        dynamicRouters(menus);
      });
    };

    const changeKeyCollapse = () => {
      keyCollapse.value = !keyCollapse.value;
    };

    const handlePopTag = (index) => {
      tags.value.splice(index, 1);
    };
    return {
      openKeys,
      selectedKeys,
      keyCollapse,
      changeKeyCollapse,
      menus,
      tags,
      handlePopTag,
      getMenus,
    };
  },
  {
    persist: true,
  }
);
